﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;

namespace WpfCustomControlLibrary4
{
    public class FlipPanel:Control
    {
        #region 01 - 属性和依赖项属性
        public static readonly DependencyProperty FrontContentProperty = DependencyProperty.Register("FrontContent", typeof(object), typeof(FlipPanel));
        public static readonly DependencyProperty BackContentProperty = DependencyProperty.Register("BackContent", typeof(object), typeof(FlipPanel));
        public static readonly DependencyProperty CornerRadiusProperty= DependencyProperty.Register("CornerRadius", typeof(CornerRadius),typeof(FlipPanel));
        //public static readonly DependencyProperty IsFlippedProperty = DependencyProperty.Register("IsFlipped", typeof(bool),typeof(FlipPanel));
        public static readonly DependencyProperty IsFlippedProperty = DependencyProperty.Register("IsFlipped", typeof(bool),typeof(FlipPanel), new() { DefaultValue = false});

        public object FrontContent
        {
            get => GetValue(FrontContentProperty);
            set { SetValue(FrontContentProperty, value); }
        }

        public object BackContent
        {
            get => GetValue(BackContentProperty);
            set { SetValue(BackContentProperty, value); }
        }

        public CornerRadius CornerRadius
        {
            get => (CornerRadius)GetValue(CornerRadiusProperty);
            set { SetValue(CornerRadiusProperty, value); }
        }

        public bool IsFlipped
        {
            get=> (bool)GetValue(IsFlippedProperty);
            set 
            { 
                SetValue(IsFlippedProperty, value);
                this.ChangeStates(true);
            }
        }
        #endregion

        static FlipPanel()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(FlipPanel), new FrameworkPropertyMetadata(typeof(FlipPanel)));

           
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            if (GetTemplateChild("FlipButton") is ToggleButton FlipButton)
            {
                FlipButton.Click += FlipButton_Click; //添加ToggleButton的点击事件
            }

            this.ChangeStates(false);//注意，默认初始化完成的时候，先不显示
        }


        private void FlipButton_Click(object sender, RoutedEventArgs e)
        {
            IsFlipped = !IsFlipped;
        }

        /// <summary>
        /// 负责切换可视化状态
        /// </summary>
        /// <param name="v"></param>
        private void ChangeStates(bool v)
        { 
            //VisualStateManager
            if (IsFlipped)
            {
                VisualStateManager.GoToState(this, "Flipped", v);
            }
            else
            {
                VisualStateManager.GoToState(this, "Normal", v);
            }
        }

    }
}
